<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="运行原理," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="&amp;#x5F15;&amp;#x8A00;:ViewController&amp;#x662F;iOS&amp;#x5F00;&amp;#x53D1;&amp;#x4E2D;MVC&amp;#x6A21;&amp;#x5F0F;&amp;#x4E2D;&amp;#x7684;C(&amp;#x89C6;&amp;#x56FE;&amp;#x63A7;&amp;#x5236;&amp;#x5668;)&amp;#xFF0C;ViewController&amp;#x662F;view&amp;#x7684;controller&amp;#x">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS开发 - 深入理解控制器与类以及view的加载">
<meta property="og:url" content="http://www.licheng244.com/posts/2016-11/08-1.html">
<meta property="og:site_name" content="LiCheng的博客">
<meta property="og:description" content="&amp;#x5F15;&amp;#x8A00;:ViewController&amp;#x662F;iOS&amp;#x5F00;&amp;#x53D1;&amp;#x4E2D;MVC&amp;#x6A21;&amp;#x5F0F;&amp;#x4E2D;&amp;#x7684;C(&amp;#x89C6;&amp;#x56FE;&amp;#x63A7;&amp;#x5236;&amp;#x5668;)&amp;#xFF0C;ViewController&amp;#x662F;view&amp;#x7684;controller&amp;#x">
<meta property="og:image" content="http://ogewbn1ek.bkt.clouddn.com/view%E7%9A%84%E6%89%A7%E8%A1%8C.png">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2996699-369abaa1787f4856.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2996699-a0cc9d4d667b034f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2996699-8770cb8869127531.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2996699-54788249191211f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2017-09-18T09:05:32.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS开发 - 深入理解控制器与类以及view的加载">
<meta name="twitter:description" content="&amp;#x5F15;&amp;#x8A00;:ViewController&amp;#x662F;iOS&amp;#x5F00;&amp;#x53D1;&amp;#x4E2D;MVC&amp;#x6A21;&amp;#x5F0F;&amp;#x4E2D;&amp;#x7684;C(&amp;#x89C6;&amp;#x56FE;&amp;#x63A7;&amp;#x5236;&amp;#x5668;)&amp;#xFF0C;ViewController&amp;#x662F;view&amp;#x7684;controller&amp;#x">
<meta name="twitter:image" content="http://ogewbn1ek.bkt.clouddn.com/view%E7%9A%84%E6%89%A7%E8%A1%8C.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://www.licheng244.com/posts/2016-11/08-1.html"/>


  <title> iOS开发 - 深入理解控制器与类以及view的加载 | LiCheng的博客 </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">LiCheng的博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-lifes">
          <a href="/lifes" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            生活
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                iOS开发 - 深入理解控制器与类以及view的加载
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-11-08T12:00:00+08:00" content="2016-11-08">
              2016-11-08
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/iOS开发-UI/" itemprop="url" rel="index">
                    <span itemprop="name">iOS开发__UI</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/posts/2016-11/08-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="posts/2016-11/08-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          


          

          
          
             <span id="/posts/2016-11/08-1.html" class="leancloud_visitors" data-flag-title="iOS开发 - 深入理解控制器与类以及view的加载">
               &nbsp; | &nbsp;
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               <span class="post-meta-item-text">阅读次数 </span>
               <span class="leancloud-visitors-count"></span>
              </span>
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p><strong>&#x5F15;&#x8A00;:</strong><br>ViewController&#x662F;iOS&#x5F00;&#x53D1;&#x4E2D;MVC&#x6A21;&#x5F0F;&#x4E2D;&#x7684;C(&#x89C6;&#x56FE;&#x63A7;&#x5236;&#x5668;)&#xFF0C;ViewController&#x662F;view&#x7684;controller&#xFF0C;ViewController&#x7684;&#x804C;&#x8D23;&#x4E3B;&#x8981;&#x5305;&#x62EC;&#x7BA1;&#x7406;&#x5185;&#x90E8;&#x5404;&#x4E2A;view&#x7684;&#x52A0;&#x8F7D;&#x663E;&#x793A;&#x548C;&#x5378;&#x8F7D;&#xFF0C;&#x540C;&#x65F6;&#x8D1F;&#x8D23;&#x4E0E;&#x5176;&#x4ED6;ViewController&#x7684;&#x901A;&#x4FE1;&#x548C;&#x534F;&#x8C03;&#x3002;</p>
</blockquote>
<a id="more"></a>
<hr>
<h3 id="&#x7B80;&#x4ECB;"><a href="#&#x7B80;&#x4ECB;" class="headerlink" title="&#x7B80;&#x4ECB;"></a>&#x7B80;&#x4ECB;</h3><p>&#x5728;IOS&#x4E2D;&#xFF0C;&#x6709;&#x4E24;&#x7C7B;ViewController:    </p>
<blockquote>
<ul>
<li><p>&#x4E00;&#x7C7B;&#x4E3B;&#x8981;&#x7528;&#x4E8E;<b>&#x5C55;&#x793A;&#x5185;&#x5BB9;</b>&#xFF0C;&#x6BD4;&#x5982;<b style="background-color:#f7eef1;color:#c75c8c">UIViewController</b>&#x3001;<b style="background-color:#f7eef1;color:#c75c8c">UITableViewController</b>&#x7B49;&#xFF0C;&#x540C;&#x65F6;&#x8FD8;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#x7EE7;&#x627F;&#x81EA;UIViewController&#x7684;UIViewController&#xFF1B;</p>
</li>
<li><p>&#x53E6;&#x4E00;&#x7C7B;&#x662F;<b>ViewController&#x5BB9;&#x5668;</b>&#xFF0C;<b style="background-color:#f7eef1;color:#c75c8c">UINavigationViewController</b>&#x548C;<b style="background-color:#f7eef1;color:#c75c8c">UITabBarController</b>&#x7B49;&#xFF0C;UINavigationController&#x662F;&#x4EE5;Stack&#x7684;&#x5F62;&#x5F0F;&#x6765;&#x5B58;&#x50A8;&#x548C;&#x7BA1;&#x7406;ViewController&#xFF0C;UITabBarController&#x662F;&#x4EE5;Array&#x7684;&#x5F62;&#x5F0F;&#x6765;&#x7BA1;&#x7406;ViewController&#x3002;</p>
</li>
</ul>
</blockquote>
<p>&#x4E0D;&#x7BA1;&#x662F;&#x54EA;&#x7C7B;ViewController,&#x90FD;&#x7EE7;&#x627F;&#x81EA;UIViewController</p>
<hr>
<h3 id="UIViewController"><a href="#UIViewController" class="headerlink" title="UIViewController"></a>UIViewController</h3><h4 id="&#x63A7;&#x5236;&#x5668;&#x4ECE;&#x521B;&#x5EFA;&#x5230;&#x9500;&#x6BC1;&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;"><a href="#&#x63A7;&#x5236;&#x5668;&#x4ECE;&#x521B;&#x5EFA;&#x5230;&#x9500;&#x6BC1;&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;" class="headerlink" title="&#x63A7;&#x5236;&#x5668;&#x4ECE;&#x521B;&#x5EFA;&#x5230;&#x9500;&#x6BC1;&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;"></a>&#x63A7;&#x5236;&#x5668;&#x4ECE;&#x521B;&#x5EFA;&#x5230;&#x9500;&#x6BC1;&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;</h4><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">load-&gt;</div><div class="line">initialize-&gt;</div><div class="line">init(initWithNibName)&#x2014;&gt;  </div><div class="line">loadView&#x2014;&gt;</div><div class="line">viewDidLoad&#x2014;&gt;</div><div class="line">viewWillApper&#x2014;&gt;</div><div class="line">viewDidApper&#x2014;&gt;  </div><div class="line">viewWillDisapper&#x2014;&gt;</div><div class="line">viewDidDisapper&#x2014;&gt;</div><div class="line">viewWillUnload-&gt;  </div><div class="line">viewDidUnload&#x2014;&gt;</div><div class="line">dealloc</div></pre></td></tr></table></figure>
<p>&#x5176;&#x4E2D;viewWillUnload<code>&#x8DDF;</code>viewDidUnLoad<code>&#x5728;iOS6&#x4EE5;&#x540E;&#x5C31;&#x8FC7;&#x671F;&#x4E86;.
&#x6536;&#x5230;</code>low-memory<code>&#x65F6;&#x7CFB;&#x7EDF;&#x4E0D;&#x4F1A;&#x91CA;&#x653E;</code>view<code>&#xFF0C;&#x800C;&#x53EA;&#x662F;&#x91CA;&#x653E;</code>controller<code>&#x7684;</code>resource`&#x3002;</p>
<h4 id="&#x6CE8;&#x610F;&#x70B9;"><a href="#&#x6CE8;&#x610F;&#x70B9;" class="headerlink" title="&#x6CE8;&#x610F;&#x70B9;:"></a>&#x6CE8;&#x610F;&#x70B9;:</h4><ul>
<li><p><code>LoadView:</code>    </p>
<ol>
<li>&#x63A7;&#x5236;&#x5668;&#x8C03;&#x7528;loadView&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x63A7;&#x5236;&#x5668;&#x7684;view.&#x5F53;&#x63A7;&#x5236;&#x5668;&#x7684;view&#x5B58;&#x5728;&#x4E86;&#x5C31;&#x4E0D;&#x4F1A;&#x8C03;&#x7528;.</li>
<li>&#x4E0D;&#x8981;&#x518D;LoadView&#x4E2D;&#x8C03;&#x7528;<code>[super loadView]</code>,&#x4F1A;&#x5F71;&#x54CD;CPU&#x6027;&#x80FD;</li>
<li>&#x82F9;&#x679C;&#x5B98;&#x65B9;&#x6587;&#x6863;:    <ul>
<li><blockquote>
<p>You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.</p>
</blockquote>
</li>
</ul>
</li>
</ol>
</li>
<li><p>&#x5F53;&#x7CFB;&#x7EDF;&#x8981;&#x5C55;&#x793A;&#x8FD9;&#x4E2A;&#x63A7;&#x5236;&#x5668;view&#x7684;&#x65F6;&#x5019;,&#x4F1A;&#x5148;&#x53BB;view&#x7684;getter&#x65B9;&#x6CD5;&#x4E2D;&#x5BFB;&#x627E;&#x6709;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;view,&#x5982;&#x679C;<code>view == nil</code>,&#x7CFB;&#x7EDF;&#x5C31;&#x4F1A;&#x4E3B;&#x52A8;&#x53BB;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;.</p>
</li>
<li><p>&#x63A7;&#x5236;&#x5668;&#x7684;view&#x90FD;&#x662F;&#x61D2;&#x52A0;&#x8F7D;,&#x5F53;&#x9700;&#x8981;&#x5C55;&#x793A;&#x7684;&#x65F6;&#x5019;&#x624D;&#x4F1A;&#x53BB;&#x521B;&#x5EFA;</p>
<ol>
<li>&#x61D2;&#x52A0;&#x8F7D;:&#x91CD;&#x5199;getter&#x65B9;&#x6CD5;</li>
<li>&#x597D;&#x5904;:&#x4E0D;&#x7528;&#x7BA1;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x9700;&#x8981;&#x521B;&#x5EFA;,&#x505A;&#x5230;&#x8981;&#x7528;&#x65F6;&#x518D;&#x521B;&#x5EFA;</li>
</ol>
</li>
</ul>
<h3 id="view&#x7684;&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;"><a href="#view&#x7684;&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;" class="headerlink" title="view&#x7684;&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;"></a>view&#x7684;&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;</h3><p>&#x8FD9;&#x91CC;&#x6307;&#x7684;View&#x662F;&#x6307;Controller&#x7684;View&#x3002;&#x5B83;&#x4F5C;&#x4E3A;Controler&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x751F;&#x547D;&#x5468;&#x671F;&#x5728;Controller&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x5185;&#x3002;&#x5C31;&#x662F;&#x8BF4;&#x4F60;&#x7684;Controller&#x4E0D;&#x80FD;&#x5728;view&#x91CA;&#x653E;&#x524D;&#x5C31;&#x91CA;&#x653E;&#x4E86;&#x3002;<br><img src="http://ogewbn1ek.bkt.clouddn.com/view%E7%9A%84%E6%89%A7%E8%A1%8C.png" alt=""></p>
<h4 id="&#x4ECE;&#x4EE3;&#x7801;&#x4E2D;&#x52A0;&#x8F7D;view"><a href="#&#x4ECE;&#x4EE3;&#x7801;&#x4E2D;&#x52A0;&#x8F7D;view" class="headerlink" title="&#x4ECE;&#x4EE3;&#x7801;&#x4E2D;&#x52A0;&#x8F7D;view"></a>&#x4ECE;&#x4EE3;&#x7801;&#x4E2D;&#x52A0;&#x8F7D;view</h4><p><img src="http://upload-images.jianshu.io/upload_images/2996699-369abaa1787f4856.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x4EE3;&#x7801;&#x521B;&#x5EFA;"></p>
<h4 id="&#x4ECE;storyboard-xib&#x4E2D;&#x521B;&#x5EFA;view"><a href="#&#x4ECE;storyboard-xib&#x4E2D;&#x521B;&#x5EFA;view" class="headerlink" title="&#x4ECE;storyboard/xib&#x4E2D;&#x521B;&#x5EFA;view"></a>&#x4ECE;storyboard/xib&#x4E2D;&#x521B;&#x5EFA;view</h4><p><img src="http://upload-images.jianshu.io/upload_images/2996699-a0cc9d4d667b034f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
<h4 id="&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;"><a href="#&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;" class="headerlink" title="&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;"></a>&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;</h4><p><img src="http://upload-images.jianshu.io/upload_images/2996699-8770cb8869127531.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="view&#x7684;&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;"></p>
<h4 id="&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x987A;&#x5E8F;"><a href="#&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x987A;&#x5E8F;" class="headerlink" title="&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x987A;&#x5E8F;"></a>&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x987A;&#x5E8F;</h4><p><code>+ (id)alloc</code><br>    &#x5206;&#x914D;&#x5185;&#x5B58;&#xFF1B;</p>
<p><code>- (id)init</code> &#x65B9;&#x6CD5;&#xFF08;&#x5305;&#x62EC;&#x5176;&#x4ED6;-(id)init&#x2026;&#x65B9;&#x6CD5;&#xFF09;<br> &#x53EA;&#x5141;&#x8BB8;&#x8C03;&#x7528;&#x4E00;&#x6B21;&#xFF0C;&#x5E76;&#x4E14;&#x8981;&#x4E0E; alloc&#x65B9;&#x6CD5; &#x5199;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x5728;init&#x65B9;&#x6CD5;&#x4E2D;&#x7533;&#x8BF7;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x8981;&#x5728;dealloc&#x65B9;&#x6CD5;&#x4E2D;&#x91CA;&#x653E;&#xFF08;&#x6216;&#x8005;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#xFF09;&#xFF1B;</p>
<p><code>- (void)awakeFromNib</code><br>&#x4F7F;&#x7528;Xib&#x521D;&#x59CB;&#x5316;&#x540E;&#x4F1A;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#xFF0C;&#x4E00;&#x822C;&#x4E0D;&#x4F1A;&#x91CD;&#x5199;&#x6B64;&#x65B9;&#x6CD5;&#xFF1B;</p>
<p><code>- (void)loadView</code><br>&#x5982;&#x679C;&#x4F7F;&#x7528;Xib&#x521B;&#x5EFA;ViewController&#xFF0C;&#x5C31;&#x4E0D;&#x8981;&#x91CD;&#x5199;&#x8BE5;&#x65B9;&#x6CD5;&#x3002;&#x4E00;&#x822C;&#x4E0D;&#x4F1A;&#x4FEE;&#x6539;&#x6B64;&#x65B9;&#x6CD5;&#xFF1B;</p>
<p><code>- (void)viewDidLoad</code><br>&#x89C6;&#x56FE;&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6B64;&#x589E;&#x52A0;&#x4E00;&#x4E9B;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x7684;&#x63A7;&#x4EF6;&#xFF0C;&#x6CE8;&#x610F;&#x6B64;&#x65F6;view&#x7684;frame&#x4E0D;&#x4E00;&#x5B9A;&#x662F;&#x663E;&#x793A;&#x65F6;&#x5019;&#x7684;frame&#xFF0C;&#x771F;&#x5B9E;&#x7684;frame&#x4F1A;&#x5728; - (void)viewDidAppear: &#x540E;&#x3002;<br>&#x5728;iOS6.0+&#x7248;&#x672C;&#x4E2D;&#x5728;&#x5BF9;&#x8C61;&#x7684;&#x6574;&#x4E2A;&#x751F;&#x547D;&#x5468;&#x671F;&#x4E2D;&#x53EA;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x4E00;&#x6B21;&#xFF0C;&#x8FD9;&#x91CC;&#x8981;&#x6CE8;&#x610F;&#x5728;iOS3.0~iOS5.X&#x7248;&#x672C;&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x91CD;&#x590D;&#x8C03;&#x7528;&#xFF0C;&#x5F53;ViewController&#x6536;&#x5230;&#x5185;&#x5B58;&#x8B66;&#x544A;&#x540E;&#xFF0C;&#x4F1A;&#x91CA;&#x653E;View&#xFF0C;&#x5E76;&#x8C03;&#x7528;viewDidUnload&#xFF0C;&#x4E4B;&#x540E;&#x4F1A;&#x91CD;&#x65B0;&#x8C03;&#x7528;viewDidLoad&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x652F;&#x6301;iOS6.0&#x4EE5;&#x524D;&#x7248;&#x672C;&#x7684;&#x7AE5;&#x978B;&#x8981;&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x7684;&#x5185;&#x5B58;&#x7BA1;&#x7406;&#x3002;</p>
<p><code>- (void)viewWillAppear:(BOOL)animated</code><br>view &#x5C06;&#x8981;&#x663E;&#x793A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6B64;&#x52A0;&#x8F7D;&#x4E00;&#x4E9B;&#x56FE;&#x7247;&#xFF0C;&#x548C;&#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x5360;&#x5185;&#x5B58;&#x7684;&#x8D44;&#x6E90;&#xFF1B;</p>
<p><code>- (void)viewDidAppear:(BOOL)animated</code><br>view &#x5DF2;&#x7ECF;&#x663E;&#x793A;&#x7684;&#x65F6;&#x5019;&#x8C03;&#x7528;&#xFF1B;</p>
<p><code>- (void)viewWillDisappear:(BOOL)animated</code><br>view &#x5C06;&#x8981;&#x9690;&#x85CF;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6B64;&#x5C06;&#x4E00;&#x4E9B;&#x5360;&#x7528;&#x5185;&#x5B58;&#x6BD4;&#x8F83;&#x5927;&#x7684;&#x8D44;&#x6E90;&#x5148;&#x91CA;&#x653E;&#x6389;&#xFF0C;&#x5728; viewWillAppear: &#x4E2D;&#x91CD;&#x65B0;&#x52A0;&#x8F7D;&#x3002;&#x5982;&#xFF1A;&#x56FE;&#x7247;/&#x58F0;&#x97F3;/&#x89C6;&#x9891;&#x3002;&#x5982;&#x679C;View&#x5DF2;&#x7ECF;&#x9690;&#x85CF;&#x800C;&#x53C8;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x4FDD;&#x7559;&#x8FD9;&#x4E9B;&#x5728;&#x663E;&#x793A;&#x524D;&#x4E0D;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x8D44;&#x6E90;&#xFF0C;&#x90A3;&#x4E48;App&#x95EA;&#x9000;&#x7684;&#x51E0;&#x7387;&#x4F1A;&#x589E;&#x52A0;&#xFF0C;&#x5C24;&#x5176;&#x662F;ViewController&#x6BD4;&#x8F83;&#x591A;&#x7684;&#x65F6;&#x5019;&#xFF1B;</p>
<p><code>- (void)viewDidAppear:(BOOL)animated</code><br>view &#x5DF2;&#x7ECF;&#x9690;&#x85CF;&#x7684;&#x65F6;&#x5019;&#x8C03;&#x7528;&#xFF1B;</p>
<p><code>- (void)dealloc</code><br>&#x4E0D;&#x8981;&#x624B;&#x52A8;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#xFF0C;&#x5F53;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#x4E3A;0&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x7CFB;&#x7EDF;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="&#x7C7B;&#x76F8;&#x5173;&#x65B9;&#x6CD5;"><a href="#&#x7C7B;&#x76F8;&#x5173;&#x65B9;&#x6CD5;" class="headerlink" title="&#x7C7B;&#x76F8;&#x5173;&#x65B9;&#x6CD5;"></a>&#x7C7B;&#x76F8;&#x5173;&#x65B9;&#x6CD5;</h3><p><code>+(void)load</code><br>&#x5F53;&#x4E00;&#x4E2A;&#x7C7B;&#x88AB;&#x52A0;&#x8F7D;&#x65F6;&#x8C03;&#x7528;,&#x53EA;&#x52A0;&#x8F7D;&#x4E00;&#x6B21;</p>
<p><code>+(void)initialize</code><br>&#x5F53;&#x672C;&#x7C7B;&#x6216;&#x8005;&#x5B50;&#x7C7B;&#x88AB;&#x52A0;&#x8F7D;&#x65F6;&#x8C03;&#x7528;,&#x53EF;&#x80FD;&#x8C03;&#x7528;&#x591A;&#x6B21;</p>
<p><code>-(instancetype)init</code><br>&#x7528;&#x4EE3;&#x7801;&#x521B;&#x5EFA;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#x8C03;&#x7528;,&#x53EA;&#x80FD;&#x505A;&#x4E00;&#x4E9B;&#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;,&#x4E0D;&#x80FD;&#x8BBE;&#x7F6E;&#x63A7;&#x4EF6;&#x7684;frame,init&#x5176;&#x5B9E;&#x662F;&#x53BB;&#x8C03;&#x7528;initWithFrame,&#x53EA;&#x4E0D;&#x8FC7;frame&#x4E3A;CGRectZero</p>
<p><code>-(instancetype)initWithFrame:(CGRect)frame</code><br>&#x7528;&#x4EE3;&#x7801;&#x521B;&#x5EFA;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#x8C03;&#x7528;,&#x53EA;&#x80FD;&#x505A;&#x4E00;&#x4E9B;&#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;,&#x4E0D;&#x80FD;&#x5728;&#x8FD9;&#x8BBE;&#x7F6E;&#x63A7;&#x4EF6;&#x7684;frame,&#x5982;&#x679C;&#x5DF2;&#x7ECF;&#x77E5;&#x9053;&#x4E86;frame,&#x90A3;&#x4E48;&#x5728;&#x8FD9;&#x91CC;&#x8BBE;&#x7F6E;&#x5B50;&#x63A7;&#x4EF6;&#x7684;frame&#x662F;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#x7684;,&#x4F46;&#x662F;&#x5982;&#x679C;&#x5916;&#x754C;&#x4F7F;&#x7528;init&#x7684;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;,&#x6700;&#x7EC8;&#x4E5F;&#x4F1A;&#x8C03;&#x7528;initWithFrame&#x65B9;&#x6CD5;,&#x6B64;&#x65F6;&#x7684;frame&#x4F20;&#x8FDB;&#x6765;&#x662F;0,&#x90A3;&#x4E48;,&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x91CC;&#x9762;&#x8BBE;&#x7F6E;&#x7684;&#x5B50;&#x63A7;&#x4EF6;&#x7684;frame&#x4E5F;&#x4F1A;&#x4E3A;0.&#x6240;&#x4EE5;,&#x4E3A;&#x4E86;&#x4E25;&#x8C28;&#x8D77;&#x89C1;,&#x6700;&#x597D;&#x4E0D;&#x8981;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x91CC;&#x9762;&#x8BBE;&#x7F6E;&#x5B50;&#x63A7;&#x4EF6;&#x7684;frame&#x3002;</p>
<p><code>-(instancetype)initWithCoder:(NSCoder *)aDecoder</code><br>&#x4ECE;xib/storyboard&#x4E2D;&#x52A0;&#x8F7D;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;,&#x53EA;&#x80FD;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x505A;&#x4E00;&#x4E9B;&#x4E00;&#x6B21;&#x6027;&#x8BBE;&#x7F6E;,&#x4E0D;&#x80FD;&#x8BBE;&#x7F6E;&#x63A7;&#x4EF6;&#x7684;frame</p>
<p><code>-(void)awakeFromNib</code><br>&#x4ECE;&#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x8F7D;.&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;,&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4E2D;&#x8BBE;&#x7F6E;frame</p>
<p><code>-(void)layoutSubviews</code><br>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x6539;&#x53D8;&#x5B50;&#x89C6;&#x56FE;&#x7684;&#x9ED8;&#x8BA4;&#x5E03;&#x5C40;&#x65F6;&#x624D;&#x9700;&#x8981;&#x53BB;&#x91CD;&#x5199; layoutSubviews &#x65B9;&#x6CD5;&#x3002;</p>
<h4 id="&#x4F7F;&#x7528;-NavigationController-&#x53BB;-Push-&#x5207;&#x6362;&#x663E;&#x793A;&#x7684;View-&#x8C03;&#x7528;&#x7684;&#x987A;&#x5E8F;"><a href="#&#x4F7F;&#x7528;-NavigationController-&#x53BB;-Push-&#x5207;&#x6362;&#x663E;&#x793A;&#x7684;View-&#x8C03;&#x7528;&#x7684;&#x987A;&#x5E8F;" class="headerlink" title="&#x4F7F;&#x7528; NavigationController &#x53BB; Push &#x5207;&#x6362;&#x663E;&#x793A;&#x7684;View, &#x8C03;&#x7528;&#x7684;&#x987A;&#x5E8F;"></a>&#x4F7F;&#x7528; NavigationController &#x53BB; Push &#x5207;&#x6362;&#x663E;&#x793A;&#x7684;View, &#x8C03;&#x7528;&#x7684;&#x987A;&#x5E8F;</h4><p>&#x4F8B;&#x5982; &#x4ECE; A &#x63A7;&#x5236;&#x5668; Push &#x663E;&#x793A; B &#x63A7;&#x5236;&#x5668;&#xFF0C;<br><code>[self.navigationController pushViewController:B animated:YES]</code><br><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="number">1.</span> &#x52A0;&#x8F7D;B&#x63A7;&#x5236;&#x5668;&#x7684;View&#xFF08;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7684;&#x8BDD;&#xFF09;&#xFF1B;</div><div class="line"><span class="number">2.</span> &#x8C03;&#x7528; A &#x7684; - (<span class="keyword">void</span>)viewWillDisappear:(<span class="built_in">BOOL</span>)animated&#xFF1B; A&#x5C06;&#x8981;&#x9690;&#x85CF;</div><div class="line"><span class="number">3.</span> &#x8C03;&#x7528; B &#x7684; - (<span class="keyword">void</span>)viewWillAppear:(<span class="built_in">BOOL</span>)animated&#xFF1B; B&#x5C06;&#x8981;&#x663E;&#x793A;</div><div class="line"><span class="number">4.</span> &#x8C03;&#x7528; A &#x7684; - (<span class="keyword">void</span>)viewDidDisappear:(<span class="built_in">BOOL</span>)animated&#xFF1B;A&#x5DF2;&#x7ECF;&#x9690;&#x85CF;</div><div class="line"><span class="number">5.</span> &#x8C03;&#x7528; B &#x7684; - (<span class="keyword">void</span>)viewDidAppear:(<span class="built_in">BOOL</span>)animated&#xFF1B; B&#x5DF2;&#x7ECF;&#x663E;&#x793A;</div></pre></td></tr></table></figure></p>
<h3 id="&#x6536;&#x5230;&#x5185;&#x5B58;&#x8B66;&#x544A;&#x7CFB;&#x7EDF;&#x6267;&#x884C;&#x6B65;&#x9AA4;"><a href="#&#x6536;&#x5230;&#x5185;&#x5B58;&#x8B66;&#x544A;&#x7CFB;&#x7EDF;&#x6267;&#x884C;&#x6B65;&#x9AA4;" class="headerlink" title="&#x6536;&#x5230;&#x5185;&#x5B58;&#x8B66;&#x544A;&#x7CFB;&#x7EDF;&#x6267;&#x884C;&#x6B65;&#x9AA4;"></a>&#x6536;&#x5230;&#x5185;&#x5B58;&#x8B66;&#x544A;&#x7CFB;&#x7EDF;&#x6267;&#x884C;&#x6B65;&#x9AA4;</h3><p><code>- (void)didReceiveMemoryWarning</code><br>iOS6.0&#x4EE5;&#x540E; &#x5185;&#x5B58;&#x4E0D;&#x591F;&#x7528;&#x65F6;,&#x4F1A;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;,&#x63A5;&#x6536;&#x5230;&#x5185;&#x5B58;&#x8B66;&#x544A;.<br><img src="http://upload-images.jianshu.io/upload_images/2996699-54788249191211f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/运行原理/" rel="tag">#运行原理</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/2016-11/03-1.html" rel="next" title="iOS开发 - 短信验证码倒计时按钮的实现">
                <i class="fa fa-chevron-left"></i> iOS开发 - 短信验证码倒计时按钮的实现
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/2016-11/10-1.html" rel="prev" title="iOS开发 -  +(void)load 和 +(void)initialize 的区别">
                iOS开发 -  +(void)load 和 +(void)initialize 的区别 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>





    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="posts/2016-11/08-1.html"
           data-title="iOS开发 - 深入理解控制器与类以及view的加载" data-url="http://www.licheng244.com/posts/2016-11/08-1.html">
      </div>
    

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="Li Cheng" />
          <p class="site-author-name" itemprop="name">Li Cheng</p>
          <p class="site-description motion-element" itemprop="description">| 写写代码 | 做做设计 | <br>记录着工作和学习中的点点滴滴 ~~~</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">17</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">8</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#UIViewController"><span class="nav-number">2.</span> <span class="nav-text">UIViewController</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#控制器从创建到销毁方法的执行顺序"><span class="nav-number">2.1.</span> <span class="nav-text">控制器从创建到销毁方法的执行顺序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#注意点"><span class="nav-number">2.2.</span> <span class="nav-text">注意点:</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#view的加载过程"><span class="nav-number">3.</span> <span class="nav-text">view的加载过程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#从代码中加载view"><span class="nav-number">3.1.</span> <span class="nav-text">从代码中加载view</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#从storyboard-xib中创建view"><span class="nav-number">3.2.</span> <span class="nav-text">从storyboard/xib中创建view</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实现过程"><span class="nav-number">3.3.</span> <span class="nav-text">实现过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法调用顺序"><span class="nav-number">3.4.</span> <span class="nav-text">方法调用顺序</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#类相关方法"><span class="nav-number">4.</span> <span class="nav-text">类相关方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#使用-NavigationController-去-Push-切换显示的View-调用的顺序"><span class="nav-number">4.1.</span> <span class="nav-text">使用 NavigationController 去 Push 切换显示的View, 调用的顺序</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#收到内存警告系统执行步骤"><span class="nav-number">5.</span> <span class="nav-text">收到内存警告系统执行步骤</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Cheng</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"licheng244344094"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = false;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = decodeURIComponent(data.url);
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title >= 0 || index_content >= 0 ){
                                isMatch = true;
								if (i == 0) {
                                    first_occur = index_content;
                                }
                            } 
							
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("SJtE0CaYCSQVrkVXmWMYWJOd-gzGzoHsz", "TfVyU54NEToMYg0SId3KELgz");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  


</body>
</html>
